package gui;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

/**
 *	@(#)GameLabel.java
 *
 *	The <code>GameLabel</code> class extends the <code>JLabel</code> class.
 *	An instance of this class has two icons - an initial one (mostly transparent)
 *	and a final one (mostly fully opaque) which can be changed to anytime.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2008/4/22
 */
public class GameLabel
extends javax.swing.JLabel
{
	/** The opaque icon. */
	protected ImageIcon opaqueIcon;

	/**
	 *	Creates a new <code>GameLabel</code> instance with the given opaque icon
	 *	and the alpha value for the primary icon.
	 *	@param icon the opaque icon
	 *	@param alpha the alpha value
	 */
	public GameLabel(ImageIcon icon, float alpha) {
		super();
		setOpaque(false);
		setIcon(icon, alpha);
	}

	/**
	 *	Creates a new <code>GameLabel</code> instance with the given opaque icon
	 *	and a standard alpha value for the primary icon.
	 *	@param icon the opaque icon
	 */
	public GameLabel(ImageIcon icon) {
		this(icon, 1.0f);
	}

	/**
	 *	Sets the icon based on the given one and the given alpha value.
	 *	@param icon the new icon
	 *	@param alpha the alpha value
	 */
	public void setIcon(ImageIcon icon, float alpha) {
		setOpaqueIcon(icon);
		setSize(icon.getIconWidth(), icon.getIconHeight());

		/* Apply the alpha value */
		BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = img.createGraphics();
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
		g.drawImage(icon.getImage(), 0, 0, null);
		setIcon(new ImageIcon(img));
	}

	/**
	 *	Sets the opaque icon to the given one.
	 *	@param icon the new opaque icon
	 */
	public void setOpaqueIcon(ImageIcon icon) {
		opaqueIcon = icon;
	}

	/**
	 *	Returns the opaque icon.
	 *	@return the opaque icon
	 */
	public ImageIcon getOpaqueIcon() {
		return opaqueIcon;
	}

	/**
	 *	Changes the icon to the opaque one.
	 */
	public void resetIcon() {
		setIcon(opaqueIcon);
	}

	/**
	 *	Returns the string representation.
	 *	@return the string representation
	 */
	@Override
	public String toString() {
		return String.format("%s [x=%d,y=%d,w=%d,h=%d]", getClass().getSimpleName(), getX(), getY(), getWidth(), getHeight());
	}
}